home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / BidirBubbleSortAlgorithm.java < prev    next >
Text File  |  1998-09-15  |  3KB  |  82 lines

  1. /*
  2.  * @(#)BidirBubbleSortAlgorithm.java    1.3 96/12/06
  3.  *
  4.  * Copyright (c) 2070, 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. /**
  32.  * A bi-directional bubble sort demonstration algorithm
  33.  * SortAlgorithm.java, Thu Oct 27 10:32:35 1994
  34.  *
  35.  * @author James Gosling
  36.  * @version     1.6f, 31 Jan 1995
  37.  */
  38. class BidirBubbleSortAlgorithm extends SortAlgorithm {
  39.     void sort(int a[]) throws Exception {
  40.     int j;
  41.     int limit = a.length;
  42.     int st = -1;
  43.     while (st < limit) {
  44.         st++;
  45.         limit--;
  46.         boolean swapped = false;
  47.         for (j = st; j < limit; j++) {
  48.         if (stopRequested) {
  49.             return;
  50.         }
  51.         if (a[j] > a[j + 1]) {
  52.             int T = a[j];
  53.             a[j] = a[j + 1];
  54.             a[j + 1] = T;
  55.             swapped = true;
  56.         }
  57.         pause(st, limit);
  58.         }
  59.         if (!swapped) {
  60.         return;
  61.         }
  62.         else
  63.         swapped = false;
  64.         for (j = limit; --j >= st;) {
  65.         if (stopRequested) {
  66.             return;
  67.         }
  68.         if (a[j] > a[j + 1]) {
  69.             int T = a[j];
  70.             a[j] = a[j + 1];
  71.             a[j + 1] = T;
  72.             swapped = true;
  73.         }
  74.         pause(st, limit);
  75.         }
  76.         if (!swapped) {
  77.         return;
  78.         }
  79.     }
  80.     }
  81. }
  82.